package com.maoning.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.maoning.product.entity.AttrEntity;
import com.maoning.product.service.AttrAttrgroupRelationService;
import com.maoning.product.service.AttrService;
import com.maoning.product.service.CategoryService;
import com.maoning.product.vo.AttrGroupRelationVo;
import com.maoning.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.maoning.product.entity.AttrGroupEntity;
import com.maoning.product.service.AttrGroupService;
import com.maoning.common.utils.PageUtils;
import com.maoning.common.utils.R;



/**
 * 属性分组
 *
 * @author ss
 * @email 361736093@qq.com
 * @date 2021-08-19 10:28:54
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrVo> list = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",list);
    }


    /**
     * 列表
     * 获取属性分组和属性关联的列表
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", list);
    }

    /**
     * 列表
     * 获取属性分组没有关联的属性列表
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                               @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     * 这里遇到的坑!post请求一定记得加requestBody,没加的话报错是No primary or default constructor found for class,以为是Lombok,但其实不是
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     * 如果查所有的话,前端会传一个catelogId为0过来
     * 这里面的service的查询语句是用函数接口写的,比较绕
     */
    @RequestMapping("/list/{catelogId}")
    public R listByCategoryId(@RequestParam Map<String, Object> params,
                   @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        System.out.println(catelogId);
        System.out.println(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		//得到当前属性分组的id,然后去写一个service去查他的完整路径
        final Long catelogId = attrGroup.getCatelogId();
        //我们假设categoryService服务中有一个方法来查询菜单路径
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
